const Router = require('@koa/router');// 路由
const router = new Router();// 实例化路由
const chat = require('@/controller/chat');// 引入对话控制器
const weather = require('@/controller/weather');// 引入天气控制器
const upload = require('@/controller/upload');// 引入上传控制器

// 对话接口
router.post('/chatMessage', chat.chatMessage.bind(chat));

// 图片上传接口
router.post('/upload/image', upload.uploadImage.bind(upload));
router.post('/upload/base64', upload.uploadBase64Image.bind(upload));

// 天气查询接口
router.get('/weather/:city', async (ctx) => {
    try {
        const { city } = ctx.params;
        if (!city) {
            ctx.body = {
                success: false,
                message: '请提供城市名称'
            };
            return;
        }

        const result = await weather.getWeatherInfo(city);
        
        if (result.success) {
            // 添加格式化的文本信息
            const formattedText = weather.formatWeatherText(result.data);
            const advice = weather.generateWeatherAdvice(result.data);
            
            ctx.body = {
                ...result,
                data: {
                    ...result.data,
                    formattedText: formattedText + advice
                }
            };
        } else {
            ctx.body = result;
        }
    } catch (error) {
        console.error('天气查询路由错误:', error);
        ctx.body = {
            success: false,
            message: '服务器内部错误'
        };
    }
});

/* // 用户相关的
const user = require('@/controller/user');
// 登录接口
router.post('/wxlogin', user.wxlogin); */

module.exports = router;